//
//  NSString+THHook.m
//  MengLiao
//
//  Created by xiuxin on 2022/7/14.
//  Copyright © 2022 BaseBusiness. All rights reserved.
//
#import "NSString+THHook.h"
#import "THDeBugTool.h"
#import "THCrashManager.h"
@implementation NSString (THHook)

+ (void)load{
#ifdef DEBUG
    return;
#endif
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
    Class cla  = NSClassFromString(@"__NSCFConstantString");
      SEL selectors[] = {
          @selector(characterAtIndex:),
          @selector(substringFromIndex:),
          @selector(substringToIndex:),
          @selector(substringWithRange:),
          @selector(stringByReplacingOccurrencesOfString:withString:),
          @selector(stringByReplacingOccurrencesOfString:withString:options:range:),
          @selector(stringByReplacingCharactersInRange:withString:),
      };
      SEL hookselectors[] = {
          @selector(zh_HookcharacterAtIndex:),
          @selector(zh_HooksubstringFromIndex:),
          @selector(zh_HooksubstringToIndex:),
          @selector(zh_HooksubstringWithRange:),
          @selector(zh_HookstringByReplacingOccurrencesOfString:withString:),
          @selector(zh_HookstringByReplacingOccurrencesOfString:withString:options:range:),
          @selector(zh_HookstringByReplacingCharactersInRange:withString:),
      };
      for (NSUInteger index = 0; index < sizeof(selectors) / sizeof(SEL); ++index) {
         SEL originalSelector = selectors[index];
         SEL swizzledSelector = hookselectors[index];
         BOOL hook = ExchangeImplementationsInTwoClasses(cla, originalSelector, [NSString class], swizzledSelector);
          if (!hook) {
              NSLog(@"拦截失败");
          }
      }
    });
}
- (unichar)zh_HookcharacterAtIndex:(NSUInteger)index {
    unichar characteristic;
    @try {
        characteristic = [self zh_HookcharacterAtIndex:index];
    }@catch (NSException *exception) {
        [[THCrashManager shareInstance] reportExp:exception];
    }@finally {
        return characteristic;
    }
}
#pragma mark - substringFromIndex:
- (NSString *)zh_HooksubstringFromIndex:(NSUInteger)from {
    NSString *subString = nil;
    @try {
        subString = [self zh_HooksubstringFromIndex:from];
    }@catch (NSException *exception) {
        [[THCrashManager shareInstance] reportExp:exception];
        subString = nil;
    }@finally {
        return subString;
    }
}
#pragma mark - substringToIndex
- (NSString *)zh_HooksubstringToIndex:(NSUInteger)to {
    NSString *subString = nil;
    @try {
        subString = [self zh_HooksubstringToIndex:to];
    }@catch (NSException *exception) {
     [[THCrashManager shareInstance] reportExp:exception];
        subString = nil;
    }@finally {
        return subString;
    }
}
#pragma mark - substringWithRange:
- (NSString *)zh_HooksubstringWithRange:(NSRange)range {
    NSString *subString = nil;
    @try {
        subString = [self zh_HooksubstringWithRange:range];
    }@catch (NSException *exception) {
      [[THCrashManager shareInstance] reportExp:exception];
        subString = nil;
    }@finally {
        return subString;
    }
}
#pragma mark - stringByReplacingOccurrencesOfString:
- (NSString *)zh_HookstringByReplacingOccurrencesOfString:(NSString *)target withString:(NSString *)replacement {
    NSString *newStr = nil;
    @try {
        newStr = [self zh_HookstringByReplacingOccurrencesOfString:target withString:replacement];
    }@catch (NSException *exception) {
         [[THCrashManager shareInstance] reportExp:exception];
        newStr = nil;
    }@finally {
        return newStr;
    }
}
#pragma mark - stringByReplacingOccurrencesOfString:withString:options:range:
- (NSString *)zh_HookstringByReplacingOccurrencesOfString:(NSString *)target withString:(NSString *)replacement options:(NSStringCompareOptions)options range:(NSRange)searchRange {
    NSString *newStr = nil;
    @try {
        newStr = [self zh_HookstringByReplacingOccurrencesOfString:target withString:replacement options:options range:searchRange];
    }@catch (NSException *exception) {
       [[THCrashManager shareInstance] reportExp:exception];
        newStr = nil;
    }@finally {
        return newStr;
    }
}
#pragma mark - stringByReplacingCharactersInRange:withString:
- (NSString *)zh_HookstringByReplacingCharactersInRange:(NSRange)range withString:(NSString *)replacement {
    NSString *newStr = nil;
    @try {
        newStr = [self zh_HookstringByReplacingCharactersInRange:range withString:replacement];
    }@catch (NSException *exception) {
        [[THCrashManager shareInstance] reportExp:exception];
        newStr = nil;
    }@finally {
        return newStr;
    }
}
@end

